<template>
  <div class="campusInformation">
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="header_box">
        <el-input
          style="width: 168px; margin-right: 24px"
          v-model="query.key.title"
          placeholder="请输入学生名字"
          clearable
        ></el-input>

        <el-button type="primary" @click="clikcUsertListSearch">
          确认搜索
        </el-button>
      </div>

      <el-table :data="initData" style="width: 100%" border>
        <el-table-column label="添加日期" width="180">
          <template slot-scope="scope">
            {{
              $moment(parseInt(scope.row.createdAt)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column label="图片" width="180">
          <template slot-scope="scope">
            <img :src="scope.row.imagesUrl" alt="" />
          </template>
        </el-table-column>
        <el-table-column prop="content" label="内容" width="180">
        </el-table-column>
        <el-table-column label="发布日期" width="180">
          <template slot-scope="scope">
            {{
              $moment(parseInt(scope.row.publishTime)).format(
                "YYYY-MM-DD HH:mm:ss"
              )
            }}
          </template>
        </el-table-column>
        <el-table-column prop="content1" label="详情内容段落1" width="180">
        </el-table-column>
        <el-table-column prop="content2" label="详情内容段落2" width="180">
        </el-table-column>
        <el-table-column prop="content3" label="详情内容段落3" width="180">
        </el-table-column>
        <el-table-column prop="content4" label="详情内容段落4" width="180">
        </el-table-column>

        <el-table-column fixed="right" label="是否显示" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.showIn"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="changeShowIn(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="
                $router.push('/admin/campus_information/add/' + scope.row.id)
              "
              type="text"
              size="small"
            >
              编辑
            </el-button>

            <el-button
              @click="getDeleteNews(scope.row.id)"
              type="text"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        style="text-align: center; margin-top: 24px"
        background
        layout="prev, pager, next"
        :total="total"
        @current-change="handleSizeChange"
        :page-size="query.pageSize"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getDeleteNews, getNewsEdit, getNewsList } from "@/util/news.js";

export default {
  data() {
    return {
      query: {
        key: {
          title: "",
        },
        pageSize: 10,
        pageNum: 1,
      },
      initData: [],
      total: 10,
    };
  },
  created() {
    this.getNewsList();
  },
  mounted() {},
  methods: {
    // 点击搜索按钮
    clikcUsertListSearch() {
      this.getNewsList();
    },
    // 获取数据
    async getNewsList() {
      let { data: res } = await getNewsList(this.query);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data.list;
      this.total = res.data.total;
    },
    // 删除数据
    async getDeleteNews(id) {
      let { data: res } = await getDeleteNews({
        id: id,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.getNewsList();
      console.log(res);
    },

    // 点击滑块修改是否发布
    async changeShowIn(e) {
      console.log(e.id, e.showIn);
      let publishTime = e.showIn == 0 ? null : new Date();
      let { data: res } = await getNewsEdit({
        id: e.id,
        showIn: e.showIn,
        publishTime: publishTime,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.getNewsList();
    },
    // 分页
    handleSizeChange(e) {
      this.query.pageNum = e;
      this.getNewsList();
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  /deep/.cell {
    max-height: 50px;
    text-overflow: -o-ellipsis-lastline;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
    img {
      height: 50px;
    }
  }
}
</style>
